﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ApmetrixDLL
{
    /**
  * Class provides Race Game data used when testing Custom Event Logging
  */
    public class CustomRPGEventData
    {
        private static int ndx1 = 0;	// index into table array
        private static int ndx2 = 0;	// index into verb array
        //private static int ndx3 = 0;	// index into obj array

        /*
         * array of "table" values
         */
        public static Apmetrix.CustomEvent[] TABLE = {
		Apmetrix.CustomEvent.USERS_TABLE, Apmetrix.CustomEvent.APPS_TABLE
	};

        /*
         * array of "verb" values
         */
        private static readonly string[][] VERB = {
		new string[] {"Bought Weapon", "Sold Weapon", "Fired Weapon", "Cast Spell", "Entered Village", "Killed Enemy", "Healed Enemy", "Invited", "Challenged", "Liked", "Accepted", "Connected", "Neighboured", "Invited NPC Conversations"},
		new string[] {"Player Start", "Player Died", "Abandoned", "Died", "Finished", "Lost", "Completed", "Quit", "Quests Completed", "Failed", "Launched", "Timed Out", "Popped Up Offerwall", "Purchased Virtual Currency with Real Money", "Primary Virtual Currency Session Start", "Primary Virtual Currency Session Spent", "Primary Virtual Currency Session Earned", "Primary Currency at Session End", "Secondary Virtual Currency Session Start", "Secondary Virtual Currency Session Spent", "Secondary Virtual Currency Session Earned", "Secondary Currency at Session End"}
	};

        ///*
        // * array of "obj" values
        // */
        //private static readonly string [][][] obj = {
        //    {
        //        {"Flaming Sword", "Axe", "Magic Staff", "Mace"},	// [0][0] users table, bought weapon
        //        {"Flaming Sword", "Axe", "Magic Staff", "Mace"},	// [0][1] users table, sold weapon
        //        {"Flaming Sword", "Axe", "Magic Staff", "Mace"},	// [0][2] users table, fired weapon
        //        {"Freeze", "Ignite", "Heal", "Kill"},				// [0][3] users table, cast spell
        //        {null},												// [0][4] users table, entered village
        //        {"Troll", "Wizard", "Mage", "Undead", "Skeleton"},	// [0][5] users table, killed enemy
        //        {"Troll", "Wizard", "Mage", "Undead", "Skeleton"},	// [0][6] users table, healed enemy
        //        {"Friend"},											// [0][7] users table, invited
        //        {"Friend"},											// [0][8] users table, challenged
        //        {"Friend"},											// [0][9] users table, liked
        //        {"Friend"},											// [0][10] users table, accepted
        //        {"Friend"},											// [0][11] users table, connected
        //        {"Friend"},											// [0][12] users table, neighboured
        //        {"Troll", "Wizard", "Mage", "Undead", "Skeleton"},	// [0][13] users table, Invited NPC Conversations
        //    },
        //    {
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][0] apps table, player start
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][1] apps table, player died
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][2] apps table, abandoned
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][3] apps table, died
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][4] apps table, finished
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][5] apps table, lost
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][6] apps table, completed
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][7] apps table, quit
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][8] apps table. quests completed
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][9] apps table, failed
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][10] apps table, launched
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][11] apps table, timed out
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][12] apps table, popped up offerwall
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][13] apps table, Purchased Virtual Currency with Real Money
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][14] apps table, Primary virtual currency at session start
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][15] apps table, Primary Virtual currency session spent
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][16] apps table, Primary virtual currency session earned
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][17] apps table, Primary currency at session end
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][18] apps table, Secondary virtual currency at session start
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][19] apps table, Secondary Virtual currency session spent
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"},	// [1][20] apps table, Secondary virtual currency session earned
        //        {"Baldurs Gate", "Dragon Age", "Dungeons and Dragons"}	// [1][21] apps table, Secondary currency at session end
        //    }
        //};	

        /*
         * array of "location" values for social media
         */
        private static readonly string[] SOCIAL_MEDIA_LOCATION = {
		"Facebook", "Google+", "Twitter", "MySpace", "Pinterest"
	};

        /*
         * array of "location" values for village
         */
        private static readonly string[] VILLAGE_LOCATION = {
		"Village 1", "Village 2", "Village 3", "Village 4", "Village 5"
	};

        /*
         * array of "location" values for real money
         */
        private static readonly string[] REAL_MONEY_LOCATION = {
		"Prompt", "Out of Currency Pop-up", "Main Menu"
	};

        /*
         * array of "location" values for game location
         */
        private static readonly string[] LEVEL_LOCATION = {
		"Level 1", "Level 2", "Level 3", "Level 4", "Level 5"
	};

        /*
         * array of "value" values for sales
         */
        private static readonly string[] SALES_VALUE = {
		"Weekend Sale", "50% Bonus Sales", "Loyal User Sale"
	};

        /*
         * array of "value" values for real money
         */
        private static readonly string[] REAL_MONEY_VALUE = {
		"$1.99 Pack", "$4.99 Pack", "$9.99 Pack"
	};

        /*
         * array of "value" values for virtual currency
         */
        private static readonly string[] VIRTUAL_CURRENCY_VALUE = {
		"#2500", "#38425", "#47125", "#14201", "#62953", "#857" 
	};

        /**
         * get custom RPG event data table 
         * @return "table" parameter for Apmetrix.logEvent() function call
         */
        public static Apmetrix.CustomEvent getTable()
        {
            ndx1 = pickRandomTable();
            return TABLE[ndx1];
        }

        /**
         * get custom RPG event data as string array containing "Verb", "obj", "Location" and "Value" 
         * @return "parameters" parameter for Apmetrix.logEvent() function call
         */
        public static string[] getParameters()
        {
            List<string> al = new List<string>();
            ndx2 = pickRandomVerb();
            string verb = VERB[ndx1][ndx2];
            al.Add(verb);

            string obj = VERB[ndx1][ndx2];
            al.Add(obj);
            string location = getLocation(verb, obj);
            al.Add(location);
            string value = getValue(verb, obj);
            al.Add(value);
            return al.ToArray();
        }

        /**
         * log parameters that were randomly picked in getParameters() call
         * @param parameters to log
         */
        public static void logParameters(string[] parameters)
        {
            string line = "data: ";
            foreach (string s in parameters)
            {
                if (s != null)
                    line += s + ", ";
                else
                    line += "null, ";
            }

            Console.WriteLine("Custom", line);
        }

        /*
         * get "Location" portion of parameter array based on verb and obj that were picked
         */
        private static string getLocation(string verb, string obj)
        {
            string location = null;
            if (obj == "Friend")
                location = pickRandom(SOCIAL_MEDIA_LOCATION);
            else if (verb == "Entered Village")
                location = pickRandom(VILLAGE_LOCATION);
            else if (verb.Contains("Real Money"))
                location = pickRandom(REAL_MONEY_LOCATION);
            else if (verb.Contains("Currency"))
                location = null;
            else if (verb == "Launched")
                location = null;
            else
                location = pickRandom(LEVEL_LOCATION);

            return location;
        }

        /*
         * get "Value" portion of parameter array based on verb and obj that were picked
         */
        private static string getValue(string verb, string obj)
        {
            string value = null;
            if (verb == "Popped Up Offerwall")
                value = pickRandom(SALES_VALUE);
            else if (verb.Contains("Real Money"))
                value = pickRandom(REAL_MONEY_VALUE);
            else if (verb.Contains("Currency"))
                value = pickRandom(VIRTUAL_CURRENCY_VALUE);
            return value;
        }

        /*
         * randomly pick from array of table values
         */
        private static int pickRandomTable()
        {
            Random ran = new Random();
            return ((int)(ran.NextDouble() * TABLE.Length));
        }

        /*
         * randomly pick from array of verb values
         */
        private static int pickRandomVerb()
        {
            Random ran = new Random();
            int value = ((int)(ran.NextDouble() * VERB[ndx1].Length));
            return value;
        }

        /*
         * randomly pick from array of obj values
         */
        private static int pickRandomobj()
        {
            Random ran = new Random();
            return ((int)(ran.NextDouble() * VERB[ndx1][ndx2].Length));
        }

        /**
         * Randomly pick index into array
         * @param array of string values
         * @return random index into array 
         */
        private static string pickRandom(string[] array)
        {
            Random ran = new Random();
            int index = (int)(ran.NextDouble() * array.Length);
            return array[index];
        }
    }
}
